﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using System.IO;
using System.Web.UI.WebControls;
using System.Text;
using System.Net.Mail;
using Microsoft.VisualBasic;
/// <summary>
/// Summary description for security
/// </summary>
public class security
{
	public security()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public string EncryptPWD(string password)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] bs = System.Text.Encoding.UTF8.GetBytes(password);
        bs = x.ComputeHash(bs);
        System.Text.StringBuilder s = new System.Text.StringBuilder();
        foreach (byte b in bs)
        {
            s.Append(b.ToString("x2").ToLower());
        }
        return s.ToString();
    }
    public string convertTimeTo_Mdy(string validtime)
    {
        string sText = Convert.ToDateTime(validtime).ToShortDateString();
        DateTime dateTest;
        DateTimeFormatInfo sFormat = new DateTimeFormatInfo();
        sFormat.ShortDatePattern = "dd/MM/yyyy";
        dateTest = Convert.ToDateTime(sText, sFormat);
        string time = dateTest.ToString("MM/dd/yyyy");
        return time;
    }
    public string convertTimeTo_dMy(string validtime)
    {
        string sText = Convert.ToDateTime(validtime).ToShortDateString();
        DateTime dateTest;
        DateTimeFormatInfo sFormat = new DateTimeFormatInfo();
        sFormat.ShortDatePattern = "MM/dd/yyyy";
        dateTest = Convert.ToDateTime(sText, sFormat);
        string time = dateTest.ToString("dd/MM/yyyy");
        return time;
    }
    public string sotien(string point_number)
    {
        string result = String.Empty;
        try
        {
            switch (point_number)
            {
                case "100":
                    result = "100000";
                    break;
                case "200":
                    result = "200000";
                    break;
                case "500":
                    result = "500000";
                    break;
            }
        }
        catch
        {
        }
        return result;
    }
    public bool UpdateAvatar(string filename, string username, string temp)
    {
        bool resutl = false;
        try
        {
            FileUpload fileupload = new FileUpload();
            string extension = Path.GetExtension(filename).ToLower();
            filename = filename.Split('.')[0];
            if (extension == ".gif" || extension == ".jpg" || extension == ".jpeg")
            {
                string fullpath = temp +"\\"+ username + "_" + filename + extension;
                fileupload.SaveAs("D:\\websites\\" + filename + extension);
                resutl = true;
            }
        }
        catch (Exception ex)
        {
            resutl = false;
        }
        return resutl;
    }
    public bool readCookie()
    {
        try
        {
            
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }
    public bool sendmail(string Emailuser, string supportEmail, string body,string subject)
    {
        bool status = false;
        try
        {
            MailMessage mailObj = new MailMessage(
            supportEmail, Emailuser, subject, body);
            mailObj.IsBodyHtml = true;
            SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com");
            SMTPServer.Credentials = new System.Net.NetworkCredential("duongdangquoc@gmail.com", "Quoc!*!@*%");
            SMTPServer.EnableSsl = true;
            SMTPServer.Send(mailObj);
            status = true;
        }
        catch (Exception ex)
        {
            status = false;
        }
        return status;
    }
    public string ReadTemplate(string template)
    {
        string content = String.Empty;
        try
        {
            using (StreamReader dr = new StreamReader(template))
            {
                content = dr.ReadToEnd();
            }
        }
        catch
        {
        }
        return content;
    }
    public string FormatMultiline(string text)
    {
        if (text == null)
        {
            return "";
        }
        string result = text;
        if (result.Length > 0)
        {
            result = result.Replace("\r\n", "\n");
            result = result.Replace("\r", "\n");
            result = result.Replace("\n", "<br />");
            return result;
        }
        else
        {
            return text;
        }
    }
    public string RemoveSpecialCharacter(string input)
    {
        const string TextToFind = "áàảãạâấầẩẫậăắằẳẵặđéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵÁÀẢÃẠÂẤẦẨẪẬĂẮẰẲẴẶĐÉÈẺẼẸÊẾỀỂỄỆÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴ";
        const string TextToReplace = "aaaaaaaaaaaaaaaaadeeeeeeeeeeeiiiiiooooooooooooooooouuuuuuuuuuuyyyyyAAAAAAAAAAAAAAAAADEEEEEEEEEEEIIIIIOOOOOOOOOOOOOOOOOUUUUUUUUUUUYYYYY";

        //string strVietNamese = "Quách Ngọc Hoàng Nguyên";
        int index = -1;
        while ((index = input.IndexOfAny(TextToFind.ToCharArray())) != -1)
        {
            int index2 = TextToFind.IndexOf(input[index]);
            input = input.Replace(input[index], TextToReplace[index2]);
        }
        return input;
    }
    public static string ResolveUrl(string relativeUrl)
    {
        if (relativeUrl == null) throw new ArgumentNullException("relativeUrl");

        if (relativeUrl.Length == 0 || relativeUrl[0] == '/' || relativeUrl[0] == '\\')
            return relativeUrl;

        int idxOfScheme = relativeUrl.IndexOf(@"://", StringComparison.Ordinal);
        if (idxOfScheme != -1)
        {
            int idxOfQM = relativeUrl.IndexOf('?');
            if (idxOfQM == -1 || idxOfQM > idxOfScheme) return relativeUrl;
        }

        StringBuilder sbUrl = new StringBuilder();
        sbUrl.Append(HttpRuntime.AppDomainAppVirtualPath);
        if (sbUrl.Length == 0 || sbUrl[sbUrl.Length - 1] != '/') sbUrl.Append('/');

        // found question mark already? query string, do not touch!
        bool foundQM = false;
        bool foundSlash; // the latest char was a slash?
        if (relativeUrl.Length > 1
            && relativeUrl[0] == '~'
            && (relativeUrl[1] == '/' || relativeUrl[1] == '\\'))
        {
            relativeUrl = relativeUrl.Substring(2);
            foundSlash = true;
        }
        else foundSlash = false;
        foreach (char c in relativeUrl)
        {
            if (!foundQM)
            {
                if (c == '?') foundQM = true;
                else
                {
                    if (c == '/' || c == '\\')
                    {
                        if (foundSlash) continue;
                        else
                        {
                            sbUrl.Append('/');
                            foundSlash = true;
                            continue;
                        }
                    }
                    else if (foundSlash) foundSlash = false;
                }
            }
            sbUrl.Append(c);
        }

        return sbUrl.ToString();
    }
}